package Interface;

import Interface.Game.JPanelGame;
import Interface.Menu.JMenuOptions;
import Interface.Menu.JMenuFichier;
import Jastermind.Data;
import Jastermind.Player;
import Various.SerialisationXML;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class JFMain extends JFrame {

    public JFMain() {
        super("Mastermind");
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.setBackground(Color.WHITE);
        this.setContentPane(new JPanelAccueil());
        this.setSize(400, 390);
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        this.setResizable(false);
        //centrer la frame
        this.setLocation((int) (screenDimension.getWidth() - this.getWidth()) / 2, (int) (screenDimension.getHeight() - this.getHeight()) / 2);
        //Personnalisation du fond d'une JOption pane
        UIManager.put("OptionPane.background", Color.WHITE);
        UIManager.put("Panel.background", Color.WHITE);

        //Menu
        JMenuBar jMenuBar = new JMenuBar();
        JMenuFichier jMenuFichier = new JMenuFichier();
        JMenuOptions jMenuOptions = new JMenuOptions();

        jMenuBar.add(jMenuFichier);
        jMenuBar.add(jMenuOptions);

        this.setJMenuBar(jMenuBar);
        this.addWindowListener(new ActionJFMain());
        this.setVisible(true);
    }

    public void setMenuEnabled(boolean enabled) {
        for (int iMenu = 0; iMenu < this.getJMenuBar().getMenuCount(); iMenu++) {
            this.getJMenuBar().getMenu(iMenu).setEnabled(enabled);
        }
    }

    public void setJPContent(JPanel jpContent) {
        jpContent.setOpaque(false);
        this.setContentPane(jpContent);
        this.validate();
    }

    public void closeJFMain() {
        if (this.getContentPane() instanceof JPanelGame) {
            ((JPanelGame) this.getContentPane()).getGame().abandonGame();
        }
        else {
            System.exit(0);
        }
    }
    public void resetJFMainSettings() {
        SerialisationXML.saveToXml(Player.getFileName(),Data.getUniqueInstance().getListPlayers());
        this.setSize(400, 390);
        this.setMenuEnabled(true);
    }

    private class ActionJFMain extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            closeJFMain();
        }
    }
}
  
 

